// JavaScript Document
var comboboxhover = null;

$(document).ready(function (e) {
    $("#languagepanel").mouseenter(function (e) {
        $("#languagemenu").slideDown("normal");
        disableSelecting();
    });
    $("#languagepanel").mouseleave(function (e) {
        $("#languagemenu").stop();
        $("#languagemenu").slideUp("fast");
        enableSelecting();
    });

    handleButton();
    handleComboBox();
    handleCheckBox();
    handleAddToLightbox();

});
var ajaxqueryhandle = null;
function ajaxquery(u, d, s, e) {
    if (ajaxqueryhandle) {
        ajaxqueryhandle.abort();
        ajaxqueryhandle = null;
    }
    ajaxqueryhandle = $.ajax({
        url: u,
        data: d,
        type: "POST",
        success: function (data) {
            s(data);
            ajaxqueryhandle = null;
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //alert("Can not connect to server!");
            e();
            ajaxqueryhandle = null;
        }
    });
    return false;
}

function disableSelecting() {
    $("body")
    .attr("unselectable", "on")
    .css({
        "user-select": "none",
        "-o-user-select": "none",
        "-moz-user-select": "none",
        "-khtml-user-select": "none",
        "-webkit-user-selec": "none"
    });
}

function enableSelecting() {
    $("body")
    .attr("unselectable", "off")
    .css({
        "user-select": "auto",
        "-o-user-select": "auto",
        "-moz-user-select": "auto",
        "-khtml-user-select": "auto",
        "-webkit-user-selec": "auto"
    });
}


function handleEssayDetail() {
    $("#essaydetailcontrol span").click(function (e) {
        var c = $(this).attr("class");
        if (c == "view") {
            $(this).css("color", "#EF2A2A");
            $("#essaydetailcontrol .hide").css("color", "#FFF");
            $("#imagelist .verticallist").show();
            $("#essaydescription").show();
            $("#imagescroller > .scroller_view").css({ "float": "right", "width": 660, "margin-right": 20 });
            scroller_update("imagescroller");
        } else {
            $(this).css("color", "#EF2A2A");
            $("#essaydetailcontrol .view").css("color", "#FFF");
            $("#imagelist .verticallist").hide();
            $("#essaydescription").hide();
            $("#imagescroller > .scroller_view").css({ "float": "none", "width": 1120, "margin": "auto", "margin-top": 30 });
            scroller_update("imagescroller");
        }
    });
}

function handleAddToLightbox() {
    $(".imagelistitem .imagelistitem_addlightbox").click(function (e) {
        var id = $(this).parents(".imagelistitem").attr("imgid");
        ajaxquery("/Lightbox/ShowAddToLightbox", { "id": id }, function (data) {
            if (data.indexOf("imginfo") > -1) {
                $("#addtolightboxpanel").html(data);
                showPanel("addtolightboxpanel");
            }
        });
    });
}

function handleButton() {
    $(".button")
	.mousedown(function (e) {
	    $(this).css({ "background-position": "bottom left" });
	    $(this).find(".button_right").css("background-position", "bottom left");
	    e.preventDefault();
	    $(this).children("input").click();
	})
	.mouseup(function (e) {
	    $(this).css({ "background-position": "top left" });
	    $(this).find(".button_right").css("background-position", "top left");
	})
	.mouseleave(function (e) {
	    $(this).css({ "background-position": "top left" });
	    $(this).find(".button_right").css("background-position", "top left");
	});

    $(".darkbackground").click(function (e) {
        hidePanel();
    });
}

function handleComboBox() {
    $(".combobox")
    .mouseenter(function (e) {
        comboboxhover = $(this);
        disableSelecting();
    })
    .mouseleave(function (e) {
        comboboxhover = null;
        enableSelecting();
    });
    $(".combobox .combobox_selected, .combobox .combobox_button").click(function (e) {
        if ($(this).parent(".combobox").find(".combobox_item").size() > 0) {
            $(this).parent(".combobox").children(".combobox_items").toggle();
        }
    });
    $(".combobox").bind("change", function (e) {
        var changed = false;
        if ($(this).find(".combobox_item").size() > 0) {
            var selected = $(this).find(".combobox_item[selected]").eq(0);
            if (selected.attr("class") == null) {
                selected = $(this).find(".combobox_item").eq(0);
                selected.parent(".combobox_items").children(".combobox_item").removeAttr("selected");
                selected.attr("selected", "");
            }
            if (selected.attr("value") != $(this).attr("value")) {
                changed = true;
                $(this).attr("value", selected.attr("value"));
                $(this).children(".combobox_selected").text(selected.text());
            }
        } else {
            $(this).attr("value", "");
            $(this).children(".combobox_selected").text("");
        }
        var input = $(this).find("input");
        if (input != null) {
            input.val($(this).attr("value"));
        }
        if (changed) {
            $(this).trigger("changed");
        }
    });
    $(document).click(function (e) {
        $(".combobox").each(function () {
            if (comboboxhover == null || comboboxhover.attr("class") == null || $(this).attr("id") != comboboxhover.attr("id")) {
                $(this).find(".combobox_items").hide();
            }
        });
    });
    handleComboBoxItem();
    $(".combobox").trigger("change");
}
function handleComboBoxItem() {
    $(".combobox_item").click(function (e) {
        $(this).parent(".combobox_items").children(".combobox_item").removeAttr("selected");
        $(this).attr("selected", "");
        $(this).parent(".combobox_items").hide();
        $(this).parents(".combobox").trigger("change");
    });
}

function handleCheckBox() {
    $(".checkbox").click(function (e) {
        $(this).trigger("toggle");
    });
    $("label").click(function (e) {
        var id = $(this).attr("for");
        if ($("#" + id).hasClass("checkbox")) {
            $("#" + id).trigger("toggle");
        }
    });
    $(".checkbox").bind("check", function (e) {
        if ($(this).attr("disabled") == null) {
            $(this).children(".checkbox_checked").show();
            $(this).attr("checked", "");
            $(this).trigger("changed");
            $(this).find("input").val("true");
        }
    })
    .bind("uncheck", function (e) {
        if ($(this).attr("disabled") == null) {
            $(this).children(".checkbox_checked").hide();
            $(this).removeAttr("checked");
            $(this).trigger("changed");
            $(this).find("input").val("false");
        }
    })
    .bind("toggle", function (e) {
        var checked = $(this).children(".checkbox_checked");
        if (checked.is(":hidden")) {
            $(this).trigger("check");
        } else {
            $(this).trigger("uncheck");
        }
    });
}


function showPanel(panelID) {
    var panel = $("#" + panelID);
    var t = panel.height() / 2;
    var l = panel.width() / 2;
    $(".darkbackground").show();
    panel.show();
    panel.css({ "left": "50%", "top": "50%", "margin-left": -l, "margin-top": -t });
    handleButton();
    handleCheckBox();
    handleComboBox();
}

function hidePanel() {
    $(".darkbackground").hide();
    $(".panel").hide();
}

function login() {

}

function logout() {
    ajaxquery("/Account/Logout", null, function (data) {
        if (data.indexOf("Guest") > -1) {
            $("#usermenupanel").html(data).trigger("changed");
        }
    });
    return false;
}

function movetopage(url) {
    window.location = url;
}

function viewimage(id) {
    ajaxquery("/Image/Detail", { "id": id }, function (data) {
        $("#pictureinfo").html(data);
        showPanel("pictureinfo");
    });
    return false;
}

